package com.github.monoliths_uni.tankcontrol;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.StrictMode;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.monoliths_uni.tankcontrol.control.HttpController;
import com.github.monoliths_uni.tankcontrol.databinding.ActivityMainBinding;
import com.kongqw.rockerlibrary.view.RockerView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'tankcontrol' library on application startup.
    static {
        System.loadLibrary("tankcontrol");
    }

    private ActivityMainBinding binding;

    private HttpController httpController;

    public void refreshLog( String result ) {
        binding.logView.append(result + "\n");
        //let text view to move to the last line.
        int offset = binding.logView.getLineCount() * binding.logView.getLineHeight();
        if (offset > binding.logView.getHeight()) {
            binding.logView.scrollTo(0, offset - binding.logView.getHeight());
        }
    }

    public void checkOnline()
    {
        httpController.sendData(String.valueOf(0));

    }

    public int getValue(RockerView.Direction direction) {
        int result = -1;
        if (direction == RockerView.Direction.DIRECTION_LEFT) {
            result = 1;
        } else if (direction == RockerView.Direction.DIRECTION_RIGHT) {

            result = 2;
        } else if (direction == RockerView.Direction.DIRECTION_UP) {
            result = 3;

        } else if (direction == RockerView.Direction.DIRECTION_DOWN) {
            result = 4;

        } else if (direction == RockerView.Direction.DIRECTION_UP_LEFT) {
            result = 5;

        } else if (direction == RockerView.Direction.DIRECTION_UP_RIGHT) {
            result = 6;

        } else if (direction == RockerView.Direction.DIRECTION_DOWN_LEFT) {
            result = 7;

        } else if (direction == RockerView.Direction.DIRECTION_DOWN_RIGHT) {
            result = 8;

        } else if (direction == RockerView.Direction.DIRECTION_CENTER) {
            result = 9;
        }
        return result;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        httpController = new HttpController();

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.logView.setMovementMethod(ScrollingMovementMethod.getInstance());

        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                httpController.sendData(String.valueOf(1));
            }
        });

        binding.clearLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.logView.setText("");
            }
        });



        binding.leftRockerView.setOnShakeListener(RockerView.DirectionMode.DIRECTION_8, new RockerView.OnShakeListener() {
            @Override
            public void onStart() {
                refreshLog("tank move start!");
            }

            @Override
            public void direction(RockerView.Direction direction) {
                httpController.sendData(String.valueOf(getValue(direction) + 20));
                refreshLog("tank move: " + direction);
            }

            @Override
            public void onFinish() {
                refreshLog("tank move stopped!");
                httpController.endSignalSendData(String.valueOf(0));
            }
        });

        binding.rightRockerView.setOnShakeListener(RockerView.DirectionMode.DIRECTION_8, new RockerView.OnShakeListener() {
            @Override
            public void onStart() {
                refreshLog("camera move start!");
            }

            @Override
            public void direction(RockerView.Direction direction) {
                httpController.sendData(String.valueOf(getValue(direction) + 30));
                refreshLog("camera move: " + direction.toString());
            }

            @Override
            public void onFinish() {
                refreshLog("camera move stopped!");
                httpController.endSignalSendData(String.valueOf(0));
            }
        });

        // Example of a call to a native method
//        TextView tv = binding.sampleText;
//        tv.setText(stringFromJNI());

    }

    /**
     * A native method that is implemented by the 'tankcontrol' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}